package com.fuli.app.service.Serviceimpl;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.ErrorInfo;
import com.fuli.app.common.Pagination;
import com.fuli.app.common.SystemContext;
import com.fuli.app.dao.UserDao;
import com.fuli.app.dto.SignInDTO;
import com.fuli.app.dto.UserDTO;
import com.fuli.app.dto.query.UserQueryDTO;
import com.fuli.app.entity.User;
import com.fuli.app.service.UserService;
import com.fuli.app.util.EntityUtils;
import com.fuli.app.util.MD5Utils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public ActionResult<String> add(UserDTO dto) {
        User user = EntityUtils.copyObjectWithInit(dto, User.class);
        if (user.getPassword() != null)
            user.setPassword(MD5Utils.encryptMD5(user.getPassword()));
        if (userDao.insert(user) == 1) {
            return new ActionResult<String>(user.getId());
        } else {
            return new ActionResult<String>(-1, "插入失败");
        }
    }

    @Override
    public ActionResult<String> delete(String id) {
        User user = userDao.get(id);
        if (user == null)
            return new ActionResult<>(id);
        userDao.delete(id, SystemContext.getUserId());
        return new ActionResult<>(id);
    }

    @Override
    public ActionResult<List<String>> beachDelete(List<String> ids) {
        userDao.beachDelete(ids, SystemContext.getUserId());
        return new ActionResult<>(ids);
    }

    @Override
    public ActionResult<String> update(UserDTO dto) {
        User user = userDao.get(dto.getId());
        if (user == null)
            return new ActionResult<>(-1, "更新失败，对象不存在");
        if (!StringUtils.isEmpty(dto.getClassId()))
            user.setClassId(dto.getClassId());
        if (!StringUtils.isEmpty(dto.getAvatarUrl()))
            user.setAvatarUrl(dto.getAvatarUrl());
        if (!StringUtils.isEmpty(dto.getNickName()))
            user.setNickName(dto.getNickName());
        if (!StringUtils.isEmpty(dto.getPassword()))
            user.setNickName(dto.getPassword());
        if (!StringUtils.isEmpty(dto.getLastLoginIp()))
            user.setNickName(dto.getLastLoginIp());
        if (!StringUtils.isEmpty(dto.getLastLoginTime()))
            user.setNickName(dto.getLastLoginTime());
        userDao.update(user);
        return new ActionResult<>(user.getId());
    }

    @Override
    public ActionResult<UserDTO> get(String id) {
        User user = userDao.get(id);
        UserDTO userDTO = EntityUtils.copyObject(user, UserDTO.class);
        return new ActionResult<>(userDTO);
    }

    @Override
    public ActionResult<Pagination<UserDTO>> pageQuery(UserQueryDTO dto) {
        Pagination pagination = new Pagination(dto.getPageNo(), dto.getPageSize());
        int count = userDao.getCountByQuery(dto);
        pagination.setCount(count);
        if (count > 0) {
            List<User> user = userDao.pageQuery(dto);
            List<UserDTO> userDTOS = EntityUtils.copyList(user, UserDTO.class);
            pagination.setRows(userDTOS);
            return new ActionResult<>(pagination);
        }
        return new ActionResult<>(pagination);
    }

    @Override
    public ActionResult<Boolean> existAccount(String account) {
        User user = userDao.findByAccount(account);
        if (user == null)
            return new ActionResult<>(false);
        return new ActionResult<>(true);
    }

    @Override
    public ActionResult<String> resetPassword(String id, String password) {
        User user = userDao.get(id);
        if (user == null)
            return new ActionResult<>(-1, "重设密码失败，该用户不存在!");
        user.setPassword(MD5Utils.encryptMD5(password));
        if (userDao.update(user) == 1) {
            return new ActionResult<>(id);
        } else {
            return new ActionResult<>(-1, "重设密码失败！");
        }
    }

    @Override
    public ActionResult<String> updateLoginInfo(String userId, String ip) {
        User user = userDao.get(userId);
        if (user == null)
            return new ActionResult<String>(-1, "重设登录信息失败，该用户不存在!");
        user.setLastLoginTime((Data) new Date());
        user.setLastLoginIp(ip);
        if (userDao.update(user) == 1) {
            return new ActionResult<String>(userId);
        } else {
            return new ActionResult<String>(-1, "重设登录信息失败！");
        }
    }

    @Override
    public ActionResult<UserDTO> getByOpenId(String openId) {
        User user = userDao.getByOpenId(openId);
        UserDTO userDTO = EntityUtils.copyObject(user, UserDTO.class);
        return new ActionResult<>(userDTO);
    }

    //添加用户积分
    public void addSignInfo(SignInDTO dto) {
        User user = userDao.get(dto.getUserId());
        user.setScore(user.getScore() + dto.getGrade());
        userDao.updateSignInfo(user);
    }

}
